﻿using System.Windows;
using System.Windows.Controls;
using DevExpress.AgDataGrid;
using DevExpress.AgDataGrid.Data;

namespace Test.Silverlight.DataGrid {
	public partial class TotalSummaries : UserControl {
		public TotalSummaries() {
			InitializeComponent();
			grid.DataSource = CarsData.DataSource;
		}
		void grid_CustomSummary(object sender, DevExpress.AgDataGrid.Data.CustomSummaryEventArgs e) {
            if (e.SummaryProcess == CustomSummaryProcess.Start) {
                decimal count = 0;
				int rowHandle = 0;
				while(grid.IsValidRowHandle(rowHandle)) {
					if(grid.Selection.IsSelected(rowHandle))
						if(((SummaryItem)e.Item).Column.Name == "Trademark")
							count++;
						else count += ((Cars)grid.GetDataRow(rowHandle)).Price;
					rowHandle++;
				}
				if(((SummaryItem)e.Item).Column.Name == "Trademark")
					e.TotalValue = count.ToString();
				else
					e.TotalValue = string.Format("${0}", count);
                e.TotalValueReady = true;
            }
        }
        void grid_SelectionChanged(object sender, DevExpress.AgDataGrid.Data.SelectionChangedEventArgs e) {
            grid.RefreshSummaries(false);
        }

        #region OptionsPanel
        void ShowCustomSummaries() {
            grid.BeginUpdate();
            try {
                grid.Totals.Clear();
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "Trademark", Title = "Selected", SummaryType = SummaryItemType.Custom });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "Price", Title = "Total", SummaryType = SummaryItemType.Custom});
            } finally {
                grid.EndUpdate();
            }
        }
        void ShowStandardSummaries() {
            grid.BeginUpdate();
            try {
                grid.Totals.Clear();
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "HP", SummaryType = SummaryItemType.Max });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "MPGCity", SummaryType = SummaryItemType.Max });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "MPGHighway", SummaryType = SummaryItemType.Max });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "Liter", SummaryType = SummaryItemType.Max });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "HP", SummaryType = SummaryItemType.Min });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "MPGCity", SummaryType = SummaryItemType.Average, DisplayFormat = "#.00" });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "MPGHighway", SummaryType = SummaryItemType.Sum });
                grid.Totals.Add(new AgDataGridSummaryItem() { FieldName = "Liter", SummaryType = SummaryItemType.Min, DisplayFormat = "#.00" });
            } finally {
                grid.EndUpdate();
            }
        }
		bool standardChecked = true;
        void rb_Checked(object sender, RoutedEventArgs e) {
            if (sender == GetStandardSummariesRadioButton((RadioButton)sender)) {
				standardChecked = true;
                ShowStandardSummaries();
            } else {
				if(!standardChecked) return;
				standardChecked = false;
                ShowCustomSummaries();
            }
        }
        #endregion
    }
}
